package com.yiqi.liebang.feature.mine.model;

import android.text.TextUtils;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.yiqi.liebang.entity.LikeUserBo;
import com.yiqi.liebang.entity.bo.FriendBo;
import com.yiqi.liebang.entity.bo.PeopleBo;
import com.yiqi.liebang.entity.bo.RegisterBo;
import com.yiqi.liebang.entity.bo.RelationshipBo;
import com.yiqi.liebang.entity.bo.SearchBo;
import com.yiqi.liebang.entity.bo.UserCardBo;
import com.yiqi.liebang.entity.bo.UserCenterBo;
import com.yiqi.liebang.entity.bo.UserDataBo;
import com.yiqi.liebang.entity.bo.UserParamBo;
import com.yiqi.liebang.feature.login.contract.LoginContract;
import com.yiqi.liebang.feature.login.dependencies.login.DaggerLoginModelComponent;
import com.yiqi.liebang.feature.mine.contract.UserContract;
import com.yiqi.liebang.feature.mine.dependencies.login.DaggerUserModelComponent;
import com.yiqi.liebang.framework.api.ApiLib;
import com.yiqi.liebang.framework.api.UserApi;

import java.io.File;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UserModel implements UserContract.Model {

    @Inject
    UserApi api;


    @Inject
    public UserModel() {
        DaggerUserModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }


    @Override
    public Observable<UserCenterBo> getUserData(UserInfoBo userInfoBo) {
        return api.getUserCenter(userInfoBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<UserCardBo> getUserCardData() {
        return api.getUserVisitingCard().compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> editUserCardData(String userHead,
                                               String userName,
                                               String userLabelId, String userIndustry, String userWorkAddress,
                                               String userDetailAddress, String userPhone,String userEmail) {

        MultipartBody.Part headFilePart = null;

        if (!TextUtils.isEmpty(userHead)){
            File headFile = new File(userHead);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            headFilePart = MultipartBody.Part.createFormData("userHead", headFile.getName(), requestFile);
        }


        RequestBody userNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), userName);
        RequestBody userLabelIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), userLabelId);
        RequestBody userIndustryBody = RequestBody.create(MediaType.parse("multipart/form-data"), userIndustry);
        RequestBody userWorkAddressBody = RequestBody.create(MediaType.parse("multipart/form-data"), userWorkAddress);
        RequestBody userDetailAddressBody = RequestBody.create(MediaType.parse("multipart/form-data"), userDetailAddress);
        RequestBody userPhoneBody = RequestBody.create(MediaType.parse("multipart/form-data"), userPhone);
        RequestBody userEmailBody = RequestBody.create(MediaType.parse("multipart/form-data"), userEmail);
        return api.editUserCard(headFilePart,  userNameBody, userLabelIdBody, userIndustryBody, userWorkAddressBody, userDetailAddressBody,userPhoneBody, userEmailBody).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> likeUser(LikeUserBo likeUserUid) {
        return api.likeUser(likeUserUid).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<Boolean> privateUser(UserInfoBo likeUserUid) {
        return api.userPrivate(likeUserUid).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<UserDataBo> getMineData() {
        return api.getMineInfo().compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }


    @Override
    public Observable<String> deleteFriend(RelationshipBo relationshipBo) {
        return api.deleteUser(relationshipBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> addBlack(RelationshipBo id) {
        return api.addBlackList(id).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

}
